<?

//Initiate Authenticate
SecurityRedirect ();
//echo"<pre>"; print_r($_SESSION);
//initiate Vars
define("PRIMARY_KEY","skada_id");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_setup_coa.php";
$FileTemplate  = "ais_setup_coa_list.html";
$TableName     = "tblcoa";
$SubHeaderTitle= "Setup Chart Of Account";
$PageSize      = 10 ;
$HTMLGridList  = "ToolGrid";
//************ FORM ACTION **************
    if (GetParam("Action","")=="Delete") {
        DeleteRecord ("txtCheck",$PageSize);
    }
//Initiate Database Table For Search;
$FNama = GetParam("coaSearch","");
$FCode = GetParam("coaCodeSearch","");
$prefixFCode = GetParam("coaprefixCodeSearch","");

//******* DATABASE FORM QUERY ***********
$SQL = "SELECT * FROM `$TableName`";
$SQLCount = "SELECT count(*) FROM `$TableName` ";


if ($FNama)
    if (strlen($WHERE)) $WHERE .= " AND "." namacc like '%".$FNama."%'";
    else $WHERE .= " namacc like '%".$FNama."%' AND ktrasl='".GetOfficeID()."'";
if ($prefixFCode)
    if (strlen($WHERE)) $WHERE .= " AND "." skada like '%".$FCode."%'";
    else $WHERE .= " skada like '".$prefixFCode."%' AND ktrasl='".GetOfficeID()."'";	
if ($FCode)
    if (strlen($WHERE)) $WHERE .= " AND "." skada like '%".$FCode."%'";
    else $WHERE .= " skada like '%".$FCode."%' AND ktrasl='".GetOfficeID()."'";

    if (strlen($WHERE)) $WHERE = " WHERE ".$WHERE;
    else $WHERE = " WHERE ktrasl='".GetOfficeID()."'";

//$WHERE .= " AND ledger = (SELECT MAX(ledger) FROM $TableName) ";

$SQL = $SQL.$WHERE;
$SQLCount = $SQLCount.$WHERE;
$SQL = $SQL." ORDER BY skada ";

//Select Department
$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
    global $DBConnection,$FileName,$TableName;
        if (GetParam("PageNum","")) {
          $StartRecord = GetParam("PageNum","")*$PageSize-$PageSize+1;
          $EndRecord   = $StartRecord+$PageSize-1;

        }
    for ($x=$StartRecord;$x<=$EndRecord;$x++) {
        if (GetParam($sequence_name.$x,"")) {
            $DBConnection->Delete("$TableName",$sequence_name.$x);
        }
    }
    header("Location: $FileName");
    exit;
}

?>
